#!/bin/sh
[ -w test.txt ]
echo $?

test -w test.txt
echo $?

EDITOR="hello"
test -z $EDITOR
echo $?

EDITOR=""
test -z $EDITOR
echo $?

EDITOR="hello"
EDITOR1="hello"
test  $EDITOR = $EDITOR1
echo $?

NUM=12
NUM1=13
NUM2=14
[ "$NUM" -eq 12 ]
echo $?

[ "$NUM1" -ge 13 -a "$NUM" -le 12 ]
echo $?

[ "$NUM2" -lt 14 -a "$NUM1" -ge 13 -o "$NUM" -ge 12 ]
echo $?

echo "test expr 1"
VALUE=hello
expr $VALUE = "hello"
echo $?

echo "test expr 2"
VALUE=hello
expr $VALUE = "hell"
echo $?
echo "expr模式匹配"
VALUE=accounts.doc
expr $VALUE : '.*'
expr $VALUE : '\(.*\)'

echo "if then else 条件测试"

echo "enter you name:"
#read NAME
if [ -z $NAME ] ; then
	echo "you did not enter any infomation"
else
	echo "$NAME"
fi


echo "\ngrep 输出检查"
echo "please in put the string that you want to search"
#read STRING
if grep -i "$STRING" ./test.txt >/dev/null 2>&1 ; then
	echo ""$STRING" is in the file test.sh"
else
	echo ""$STRING"is not in the file test.sh"
fi

echo "\n用变量测试grep输出"
echo "Enter a list of names:"
#read LIST
if echo $LIST | grep "peter"  > /dev/null  ; then
	echo "peter is here"
else
	"peter is not in the list"
fi

echo "\n文件拷贝输出检查"
if cp test.txt testcp.txt > /dev/null ; then
	echo "good copy"
#else
	echo "`basename $0`:error coild not copy the file" 
fi


echo "\n当前目录测试"
DIRECTORY=`pwd`
if [ "$DIRECTORY" != "/" ] ; then
	echo "not in "$DIRECTORY""
	#exit with a value of 1, an error
	#exit 1;
fi

echo "\n文件权限测试"
LOGFILE=test.txt
echo $LOGFILE
if [ ! -w "$LOGFILE" ] ; then
	echo "you can not wirte to $LOGFILE "
else
	echo "you can write to $LOGFILE"
fi


echo "测试传递到脚本中的参数"
if [ $# -lt 3 ] ; then
	echo "Usage:`basename $0` arg1 arg2 arg3"
else
	echo "arg1:$1"
	echo "arg2:$2"
	echo "arg3:$3"
fi

echo "\n将脚本参数传如系统命令"
DIRECTORY=$1
#if [ "`ls -A $DIRECTORY`" = "" ] ; then
if [ -z "`ls -A $DIRECTORY`" ] ; then
	#if it's an empty string, then it is empty
	echo "$DIRECTORY is indeed empty"
else 
	echo "$DIRECTORY is not empty"
fi

echo "\n测试目录创建结果"
DIRECTORY=$1
if [ -z $DIRECTORY ] ; then 
	echo "Usage:`basename $0` directory to create"
	#exit 1
fi
if [ -d $DIRECTORY ] ; then
	echo "$DIRECTORY is a dir"
	else
		echo "the directory does exist"
		echo -n "Create it now? [y..n] :"
		read ANS
		if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ] ; then
			echo "Create now"
			mkdir $DIRECTORY > /dev/null 2>&1
			if [ $? != 0 ] ; then
				echo "Error creating the directory $DIRECTORY" >&2
				exit 1
			fi
		else :
		fi
fi

echo "\n检测最后命令状态"
DIR_NAME=testDir
THERE=`pwd`
mkdir $DIR_NAME > /dev/null
if [ -d $DIR_NAME ] ; then
	cd $DIR_NAME
	if [ $? = 0 ] ; then
		HERE=`pwd`
		cp $THERE/*.txt $HERE
	else
		echo "Can not cd to $DIR_NAME"
		exit 1
	fi
else
	echo "Cna not create dir "$DIR_NAME""
	exit 1
fi



#echo "\n简单的安全登录脚本,一种登录脚本的基本框架"
#INVALID_USR=yes
#INVALID_PASSWD=yes
#save the current stty settings
#SAVEDSTTY=`stty -g`
#echo "you are logging into a sensitive area"
#echo  "enter your id name"
#read NAME
#hide the characters typed in
#stty -echo
#echo "enter your password:"
#read PASSWORD
#back on again
#stty $SAVEDSTTY
#if [ "$NAME = "dave" ] || [ "$NAME" = "pauline" ] ; then
#	#if a valid then set variable
#	INVALID_USR=no
#fi
#if [ "$PASSWORD" = "mayday" ] ; then
#	#if valid password then set variable
#	INVALID_PASSWD=no
#fi

#if [ "$INVALID_USR" = "yes" -o "$INVALID_PASSWD" = "yes" ] ; then
#	echo "`basename $0 :` Sorry wrong password or userid"
#fi
#if we get here then their id and password are ok.
#echo "corrert usr id and password given"



echo "\ncase语句测试"
echo "enter a number from 1 to 5"
#read ANS
case $ANS in
	1) echo "you select 1"
		;;
	2) echo "you select 2"
		;;
	3) echo "you select 3"
		;;
	4) echo "you select 4"
		;;
	5) echo "you select 5"
		;;
	*)echo "`basename $0`: this is not between 1 and 5"
		;;
esac

echo "\n捕获输入并执行空命令"
TYPE=""
echo "enter the account dept No::"
#read ACC
case $ACC in
	123);;
	234);;
	456);;
	789);;
	*) echo "`basename $0`: Unknown dept No:"
		echo "try..123,234,456,789"
		;;
esac

echo "\nfor循环测试"
for loop in 1 2 3 4 5 5 6 7 8
do
	echo $loop
done

echo "\n打印字符串列表"
for loop in "orange red blue white grey"
do 
	echo $loop
done

echo "\n打印字符串列表1"
for loop in orange red blue white grey
do 
	echo $loop
done

echo "\n打印ls命令"
for loop in `ls`
do 
	echo $loop
done

echo "\n对for循环使用参数"
for param
do
	echo "you supplied $param as a command line option"
done
echo $param

echo "\n对for循环使用参数1"
for param in "$@"
do
	echo "you supplied $param as a command line option"
done
echo $param

echo "\n\n\n"
for loop
do
	find ./ -name $loop -print
done


echo "\n使用for循环链接服务器"
HOST="ladpd ladware"
for loop in $HOST
do
	ping -c 2 $loop
done



echo "\n多sed删除操作"
for files in `ls *.txt`
do
	sed -e "/^$/d" $files > $files.HOLD
	cp $files.HOLD $files
done
echo "\n循环计数"
counter=0
for files in *
do
	# increment
	counter=`expr $counter + 1`
done
echo "there are $counter files in `pwd` we need to process"

echo "\nfor循环和本地文档"
WHOS_ON=`who -u | awk '{print $1}'`
for user in $WHOS_ON
do
	mail $user << MAYDAY
	dear Collegues,
	it's my birthday today, see you down the club at 17:30 for a drink.
done
